package com.strange.mdl.module.mdl.service.sutras;

import cn.hutool.core.collection.CollUtil;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.volumes.MdlSutraVolumesPageReqVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.volumes.MdlSutraVolumesRespVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.volumes.MdlSutraVolumesSaveReqVO;
import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutraVolumesDO;
import com.strange.mdl.module.mdl.dal.mysql.sutras.MdlSutraVolumesMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.SUTRA_VOLUMES_NOT_EXISTS;

/**
 * 经书卷 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class MdlSutraVolumesServiceImpl implements MdlSutraVolumesService {

    @Resource
    private MdlSutraVolumesMapper sutraVolumesMapper;

    @Override
    public Long createSutraVolumes(MdlSutraVolumesSaveReqVO createReqVO) {
        // 插入
        MdlSutraVolumesDO sutraVolumes = BeanUtils.toBean(createReqVO, MdlSutraVolumesDO.class);
        sutraVolumesMapper.insert(sutraVolumes);
        // 返回
        return sutraVolumes.getId();
    }

    @Override
    public void updateSutraVolumes(MdlSutraVolumesSaveReqVO updateReqVO) {
        // 校验存在
        validateSutraVolumesExists(updateReqVO.getId());
        // 更新
        MdlSutraVolumesDO updateObj = BeanUtils.toBean(updateReqVO, MdlSutraVolumesDO.class);
        sutraVolumesMapper.updateById(updateObj);
    }

    @Override
    public void deleteSutraVolumes(Long id) {
        // 校验存在
        validateSutraVolumesExists(id);
        // 删除
        sutraVolumesMapper.deleteById(id);
    }

    @Override
    public void deleteSutraVolumesListByIds(List<Long> ids) {
        // 校验存在
        validateSutraVolumesExists(ids);
        // 删除
        sutraVolumesMapper.deleteByIds(ids);
    }

    private void validateSutraVolumesExists(List<Long> ids) {
        List<MdlSutraVolumesDO> list = sutraVolumesMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SUTRA_VOLUMES_NOT_EXISTS);
        }
    }

    private void validateSutraVolumesExists(Long id) {
        if (sutraVolumesMapper.selectById(id) == null) {
            throw exception(SUTRA_VOLUMES_NOT_EXISTS);
        }
    }

    @Override
    public MdlSutraVolumesDO getSutraVolumes(Long id) {
        return sutraVolumesMapper.selectById(id);
    }

    @Override
    public PageResult<MdlSutraVolumesDO> getSutraVolumesPage(MdlSutraVolumesPageReqVO pageReqVO) {
        return sutraVolumesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MdlSutraVolumesDO> getSutraVolumesList(Long sutraId) {
        return sutraVolumesMapper.selectList(sutraId);
    }

}
